<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="bannerName">
      <el-input v-model="dataForm.bannerName" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="来源类型" prop="sourceType">
      <el-input v-model="dataForm.sourceType" placeholder="来源类型"></el-input>
    </el-form-item>
    <el-form-item label="来源地址" prop="sourceUrl">
      <el-input v-model="dataForm.sourceUrl" placeholder="来源地址"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="图片地址" prop="bannerImage">
      <el-input v-model="dataForm.bannerImage" placeholder="图片地址"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          bannerId: 0,
          bannerName: '',
          sourceType: '',
          sourceUrl: '',
          createTime: '',
          bannerImage: ''
        },
        dataRule: {
          bannerName: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          sourceType: [
            { required: true, message: '来源类型不能为空', trigger: 'blur' }
          ],
          sourceUrl: [
            { required: true, message: '来源地址不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          bannerImage: [
            { required: true, message: '图片地址不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.bannerId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.bannerId) {
            this.$http({
              url: this.$http.adornUrl(`/blog/banner/info/${this.dataForm.bannerId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.bannerName = data.banner.bannerName
                this.dataForm.sourceType = data.banner.sourceType
                this.dataForm.sourceUrl = data.banner.sourceUrl
                this.dataForm.createTime = data.banner.createTime
                this.dataForm.bannerImage = data.banner.bannerImage
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/blog/banner/${!this.dataForm.bannerId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'bannerId': this.dataForm.bannerId || undefined,
                'bannerName': this.dataForm.bannerName,
                'sourceType': this.dataForm.sourceType,
                'sourceUrl': this.dataForm.sourceUrl,
                'createTime': this.dataForm.createTime,
                'bannerImage': this.dataForm.bannerImage
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
